<include file="public/header" />
<include file="public/left" />
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title"><a class="back" href="javascript:history.back();" title="返回列表">返回上一页</a>
            <div class="subject">
                <h3>添加商户</h3>
                <h5>网站系统权限资源管理</h5>
            </div>
        </div>
    </div>
    <form method="post" enctype="multipart/form-data">
        <img src="__STATIC__/admin/images/timg.gif" id="loading" style="position: absolute;left:50%;top:40%;margin: -50px 0 0 -100px;display:none;">
        <section>
            <ul class="ulColumn2">
                <li>
                    <span class="item_name" style="width:120px;">电话：</span>
                    <input type="text" name="mobile" class="textbox textbox_295" value=""/>
                </li>
                <li>
                    <span class="item_name" style="width:120px;">姓名：</span>
                    <input type="text" name="realname" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">身份证：</span>
                    <input type="text" name="idcard" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">银行卡号码：</span>
                    <input type="text" name="bankcard" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">店铺名字：</span>
                    <input type="text" name="shopname" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">省：</span>
                    <select id="s_province" name="province"></select>
                </li>
                <li>
                    <span class="item_name" style="width:120px;">市：</span>
                    <select id="s_city" name="city" ></select>
                </li>
                <li>
                    <span class="item_name" style="width:120px;">区：</span>
                    <select id="s_county" name="county"></select>
                </li>
                <li>
                    <span class="item_name" style="width:120px;">详细地址：</span>
                    <input type="text" name="address" class="textbox textbox_295" />
                </li>

                <li>
                    <span class="item_name" style="width:120px;">身份证正面：</span>
                    <input type="file" name="id_positive" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">身份证反面：</span>
                    <input type="file" name="id_reverse" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">手持身份证：</span>
                    <input type="file" name="id_hold" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">营业执照：</span>
                    <input type="file" name="license" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;">食品经营许可证：</span>
                    <input type="file" name="foodlicense" class="textbox textbox_295" />
                </li>
                <li>
                    <span class="item_name" style="width:120px;"></span>
                    <input type="button" class="link_btn1 link_btn" value="提交" />
                </li>
            </ul>
        </section>
    </form>

    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=VWIBZ-2VN3U-W5OVY-2F52K-ULONJ-MUB6B"></script>
    <script type="text/javascript" src="__STATIC__/admin/js/jquery-ui-1.10.3.min.js"></script>
    <script src="__STATIC__/admin/js/area.js"></script>
    <script type="text/javascript">_init_area();</script>
    <script>
        $(".link_btn1").click(function(){
            var formData = new FormData();
            var mobile = $('input[name="mobile"]').val();
            if(mobile == '' || !(/^1[3456789]\d{9}$/.test(mobile))){
                alert('请填写电话！');
                return;
            }
            var realname = $('input[name="realname"]').val();
            if(realname == ''){
                alert('请填写昵称！');
                return;
            }
            var idcard = $('input[name="idcard"]').val();
            var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            if(idcard == '' || reg.test(idcard) === false){
                alert('请填写身份证！');
                return;
            }
            var bankcard = $('input[name="bankcard"]').val();
            if(bankcard == ''){
                alert('银行卡号码');
                return;
            }
            var shopname = $('input[name="shopname"]').val();
            if(shopname == ''){
                alert('商铺名称');
                return;
            }
            var address = $('input[name="address"]').val();
            if(address == ''){
                alert('详细地址为空!');
                return;
            }
            var province = $('select[name="province"] option:selected').val();
            if(province == '' || province=='省份'){
                alert('请填写省份');
                return;
            }
            var city = $('select[name="city"] option:selected').val();
            if(city == '' || city == '地级市'){
                alert('请填写市');
                return;
            }
            var county = $('select[name="county"] option:selected').val();
            if(county == ''){
                alert('请选择区');
                return;
            }

            var id_positive = $(':file')[0].files[0];
            var id_reverse = $(':file')[1].files[0];
            var id_hold = $(':file')[2].files[0];
            var license = $(':file')[3].files[0];
            var foodlicense = $(':file')[4].files[0];

            var img = false;
            for(var i=0;i<=4;i++){
                if(typeof($(':file')[i].files[0]) == 'undefined'){
                    img = true;
                    break;
                }
            }

            if(img){
                alert('请上传证件图片！');
                return;
            }

            formData.append('mobile',mobile);
            formData.append('realname',realname);
            formData.append('idcard',idcard);
            formData.append('bankcard',bankcard);
            formData.append('shopname',shopname);
            formData.append('address',address);
            formData.append('province',province);
            formData.append('city',city);
            formData.append('county',county);
            formData.append('id_positive',id_positive);
            formData.append('id_reverse',id_reverse);
            formData.append('id_hold',id_hold);
            formData.append('license',license);
            formData.append('foodlicense',foodlicense);

            var address_info = province+","+city+","+county+","+address;
            var lat = '';
            var lng = '';

            $("#loading").show();

            $.ajax({
                type: "get",
                dataType: 'jsonp',
                data: {
                    key: "VWIBZ-2VN3U-W5OVY-2F52K-ULONJ-MUB6B", // 填申请到的key
                    address: address_info, //具体的地址
                    output: 'jsonp' //返回格式：支持JSON/JSONP，默认JSON
                },
                jsonp: "callback",
                jsonpCallback: "QQmap",
                url: "https://apis.map.qq.com/ws/geocoder/v1/",
                success: function (json) {
                    if(json.status == 0) {

                        formData.append('lat',json.result.location.lat);
                        formData.append('lng',json.result.location.lng);

                        $.ajax({
                            url:"{:url('Merchant/add_merchant')}",
                            type:'POST',
                            data:formData,
                            async:false,
                            processData:false,
                            contentType:false,
                            success:function(result){
                                if(!result.bool){
                                    alert(result.message);
                                    $("#loading").hide();
                                    return;
                                }

                                location.href = "{:url('merchant/index')}";
                            }
                        });
                    }else{
                        alert('请填写正确地址!');
                    }
                },
                error: function (err) {

                }
            });

        });
    </script>
</div>
</section>
</body>

</html>